package cn.com.lyj6851.modules.shop.controller;


import cn.com.lyj6851.constant.ShopConstants;
import cn.com.lyj6851.constant.SystemConfigConstants;
import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.mp.config.WxMpConfiguration;
import cn.com.lyj6851.modules.shop.domain.YdySystemConfig;
import cn.com.lyj6851.modules.shop.service.YdySystemConfigService;
import cn.com.lyj6851.modules.shop.service.dto.YdySystemConfigQueryCriteria;
import cn.com.lyj6851.utils.RedisUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags = "商城:配置管理")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("api")
public class SystemConfigController {
    private final YdySystemConfigService systemConfigService;

    @AopLog("查询")
    @ApiOperation(value = "查询")
    @GetMapping(value = "/ydySystemConfig")
    @PreAuthorize("hasAnyRole('admin','SYSTEMCONFIG_ALL','SYSTEMCONFIG_SELECT')")
    public ResponseEntity getYxSystemConfigs(YdySystemConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(systemConfigService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AopLog("新增或修改")
    @ApiOperation(value = "新增或修改")
    @PostMapping(value = "/ydySystemConfig")
    @CacheEvict(cacheNames = ShopConstants.LYJ_SHOP_REDIS_INDEX_KEY,allEntries = true)
    @PreAuthorize("hasAnyRole('admin','SYSTEMCONFIG_ALL','SYSTEMCONFIG_CREATE')")
    public ResponseEntity create(@RequestBody String jsonStr){
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        jsonObject.forEach((key,value)->{
            YdySystemConfig systemConfig = systemConfigService.getOne(new LambdaQueryWrapper<YdySystemConfig>()
                    .eq(YdySystemConfig::getMenuName,key));
            YdySystemConfig ydySystemConfig = new YdySystemConfig();
            ydySystemConfig.setMenuName(key);
            ydySystemConfig.setValue(value.toString());
            //重置微信相关 WECHAT_APPID = "wechat_appid"
            if(SystemConfigConstants.WECHAT_APPID.equals(key)){
                WxMpConfiguration.removeWxMpService();
            }
            //微信配置存储
            RedisUtil.set(key,value.toString(),0);
            if(ObjectUtil.isNull(systemConfig)){//如果数据没有
                systemConfigService.save(ydySystemConfig);
            }else {// 根据id更新
                ydySystemConfig.setId(systemConfig.getId());
                systemConfigService.saveOrUpdate(ydySystemConfig);
            }
        });

        return new ResponseEntity<>(HttpStatus.CREATED);
    }

}
